//const commonUrl = "https://whjuyi.picp.vip/api/";   	//本地测试路径
const commonUrl = "https://www.whtcyw.com:9601/api/";   	//正式路径 
// post请求封装
function postRequest(url, data) {
    var promise = new Promise((resolve, reject) => {
        var that = this;
        var postData = data;
        uni.request({
            url: commonUrl + url,
			timeout: 10000, // 设置超时时间为10秒
            data: postData,
			sslVerify:false,
            method: "POST",
            header: {
                "content-type": "application/json"
                //token: uni.getStorageSync("token")
            },
            success: function(res) {
                if (res.statusCode == 200) {
                    resolve(res.data);
                } else {
                    resolve(res.data);
                }
            },
            error: function(e) {
                reject("接口调用error！"+e);
            },fail:function(res) {
				reject("接口调用fail！"+JSON.stringify(res.errMsg));
            }
        });
    });
    return promise;
}

// post请求封装
function postRequestWithToken(url, data) {
    var promise = new Promise((resolve, reject) => {
        var that = this;
        var postData = data;
		//console.info("postData ："+postData)
        uni.request({
            url: commonUrl + url,
			timeout: 10000, // 设置超时时间为10秒
            data: postData,
			sslVerify:false,
            method: "POST",
            header: {
                "content-type": "application/json",
                token: uni.getStorageSync("userInfo").token
            },
            success: function(res) {
				//console.info("postRequestWithToken ："+JSON.stringify(res));
                if (res.statusCode == 200) {
					if (typeof res.data === 'string')
						resolve(JSON.parse(res.data));
					else
						resolve(res.data);
                } else {
                    if (typeof res.data === 'string')
                    	resolve(JSON.parse(res.data));
                    else
                    	resolve(res.data);
                }
            },
            error: function(e) {
                reject("接口调用error！"+e);
            },fail:function(res) {
				reject("接口调用fail！"+JSON.stringify(res.errMsg));
            }
        });
    });
    return promise;
}

// post请求封装
function postFileWithToken(url,files,data) {
    var promise = new Promise((resolve, reject) => {
        var that = this;
        var postData = data;
		//console.info("postData ："+JSON.stringify(postData));
		//console.info("files ："+JSON.stringify(files));
        uni.uploadFile({
            url: commonUrl + url,
			timeout: 10000, // 设置超时时间为10秒
            files: files,
            formData: postData,
            header: {
                token: uni.getStorageSync("userInfo").token
            },
            success: function(res) {
				console.info("postFileWithToken ："+JSON.stringify(res));
			   if (res.statusCode == 200) {
					if (typeof res.data === 'string')
						resolve(JSON.parse(res.data));
					else
						resolve(res.data);
			   } else {
				   if (typeof res.data === 'string')
					resolve(JSON.parse(res.data));
				   else
					resolve(res.data);
			   }
            },
            error: function(e) {
                reject("接口调用error！"+e);
            },fail:function(res) {
				reject("接口调用fail！"+JSON.stringify(res.errMsg));
            }
        });
    });
    return promise;
}

// get请求封装
function getRequest(url, data) {
    var promise = new Promise((resolve, reject) => {
        var that = this;
        var postData = data;
        uni.request({
            url: commonUrl + url,
			timeout: 10000, // 设置超时时间为10秒
            data: postData,
            method: "GET",
            dataType: "json",
            header: {
                "content-type": "application/json"
            },
            success: function(res) {
                if (res.statusCode == 200) {
                    resolve(res.data);
                } else {
                    resolve(res.data);
                }
            },
			error: function(e) {
			    reject("接口调用异常！");
			},fail:function(res) {
				reject("接口调用异常！");
			}
        });
    });
    return promise;
}

// get请求封装
function getRequestWithToken(url, data) {
    var promise = new Promise((resolve, reject) => {
        var that = this;
        var postData = data;
        uni.request({
            url: commonUrl + url,
			timeout: 10000, // 设置超时时间为10秒
            data: {
                    data: postData
                },
            method: "GET",
            dataType: "json",
            header: {
				token: uni.getStorageSync("userInfo").token
            },
            success: function(res) {
                if (res.statusCode == 200) {
                    resolve(res.data);
                } else {
                    resolve(res.data);
                }
            },
			error: function(e) {
			    reject("接口调用异常！");
			},fail:function(res) {
				reject("接口调用异常！");
			}
        });
    });
    return promise;
}

export default {
    post: postRequest,
    get: getRequest,
	post_token:postRequestWithToken,
	post_file_token:postFileWithToken,
	get_token:getRequestWithToken
};